#include "ControlBar.h"
#include "LMButton.h"

const int K_MARGIN = 10;

ControlBar::ControlBar(WindowManager* w)
{
	window = w;
	maxHeight = 0;
	controls.push_back(createText("Exit"));
	controls.push_back(createText("Clear"));
	controls.push_back(createText("|<<"));
	controls.push_back(createText("|<"));
	controls.push_back(createText("||"));
	controls.push_back(createText(">|"));
	controls.push_back(createText(">>|"));
	controls.push_back(createText(""));
	for (int i = 0; i < controls.size(); i++)
	{
		boundaries.push_back(getXend(i));
	}
	pause = true;
}


ControlBar::~ControlBar(void)
{
}

sf::Text ControlBar::createText(std::string string)
{

	//text.setCharacterSize(90);
	sf::Text text(string, *window->getFont(), 40);
	sf::FloatRect textRect = text.getLocalBounds();
	//text.seto
	
	text.setOrigin(textRect.width/2, textRect.height/2);
	text.rotate(180);
	textRect = text.getLocalBounds();
	if (textRect.height > maxHeight)
	{
		maxHeight = textRect.height;
	}

	return text;

}

void ControlBar::drawBar()
{
	for (int i = 0 ; i < controls.size(); i++)
	{
		sf::FloatRect textRect = controls[i].getLocalBounds();
		
		controls[i].setPosition(window->getOffset()+boundaries.at(i)+textRect.width/2+K_MARGIN, maxHeight);
		controls[i].setColor(window->getBackground()->getTextColour());
		window->draw(controls[i]);
		if (i != controls.size()-1)
		{
			sf::Vertex line[2] = {sf::Vertex(sf::Vector2f(window->getOffset()+boundaries.at(i), K_MARGIN), window->getBackground()->getTextColour()), sf::Vertex(sf::Vector2f(window->getOffset()+boundaries.at(i), maxHeight*1.5), window->getBackground()->getTextColour())};
			window->draw(line, 2, sf::Lines);
		}
	}

}

bool ControlBar::checkClicked(sf::Vector2i loc)
{
	if (loc.y > maxHeight*1.5)
	{
		return false;
	}
	else
	{
		for (int i = 0; i < boundaries.size(); i++)
		{
			if (loc.x >= boundaries.at(i)+window->getOffset())
			{
				current = i;
				return true;
			}
		}
		return false;
	}
}


float ControlBar::getXend(int idx)
{
	float end = window->getWidth();
	for (int i = 0; i <= idx; i++)
	{
		end -= (controls[i].getLocalBounds().width + (2*K_MARGIN));
	}
	return end;
}


Control ControlBar::clicked(std::vector<int>* npcOrder, NPC* npcs)
{
	switch (current)
	{
		//EXIT
		case 0:
			return EXIT;
		//CLEAR
		case 1:
			return CLEAR;
		//BACK
		case 2:
			if (npcOrder->size() > 0)
			{
				int latest = npcOrder->at(npcOrder->size()-1);
				npcs[latest].reset();
				npcOrder->erase(npcOrder->begin() + npcOrder->size()-1);
			}
			return BACK;
		//PREVIOUS
		case 3:
			return PREVIOUS;
			break;
		//PAUSE
		case 4:
			if (pause)
			{
				controls.at(current).setString(">");
			}
			else
			{
				controls.at(current).setString("||");
			}
			pause = !pause;
			return PAUSE;
			break;
		//NEXT
		case 5:
			return NEXT;
			break;
		//SKIP
		case 6:
			return SKIP;
			break;

	}
	return NOTHING;
}


Control ControlBar::clicked(NPC* npc)
{
	switch (current)
	{
		//EXIT
		case 0:
			return EXIT;
		//CLEAR
		case 1:
			return CLEAR;
		//BACK
		case 2:
			npc->reset();
			return BACK;
		//PREVIOUS
		case 3:
			npc->previousMessage();
			return PREVIOUS;
			break;
		//PAUSE
		case 4:
			if (pause)
			{
				controls.at(current).setString(">");
			}
			else
			{
				controls.at(current).setString("||");
			}
			pause = !pause;
			return PAUSE;
			break;
		//NEXT
		case 5:
			npc->nextMessage();
			return NEXT;
			break;
		//SKIP
		case 6:
			npc->finishNPC();
			return SKIP;
			break;
	}
	return NOTHING;
}